using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Microsoft.PowerShell.CmdletManagement
{
    [System.Windows.Markup.ContentProperty("Label")]
    public partial class ExtendedTextBlock : System.Windows.Controls.UserControl
    {
        public ExtendedTextBlock()
        {
            InitializeComponent();

            UserInput.Background = SystemColors.ControlBrush;
            UserInput.IsReadOnly = true;
        }

        public string Label
        {
            get { return (string) GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        public string Value
        {
            get { return (string) GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public string HelpText
        {
            get { return (string) GetValue(HelpTextProperty); }
            set { SetValue(HelpTextProperty, value); }
        }

        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        public string WarningValue
        {
            get { return warningValue; }
            set { warningValue = value; }
        }
        private string warningValue = "dba32e8c-6cb7-442b-abf3-3a24b08a71cc";

        private void ValueChanged(object sender, RoutedEventArgs e)
        {
            if (Value != warningValue)
            {
                Binding binding = BindingOperations.GetBinding(UserInput, TextBox.TextProperty);

                if (binding != null)
                {
                    binding.ValidationRules.Clear();
                }
            }
            else
            {
                Binding binding = BindingOperations.GetBinding(UserInput, TextBox.TextProperty);

                if (binding != null)
                {
                    binding.ValidationRules.Clear();
                    binding.ValidationRules.Add(new TriStateValidationRule());
                }
            }
        }

        private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ExtendedTextBlock textBlock = d as ExtendedTextBlock;

            if (((bool) e.NewValue) == true)
            {
                textBlock.UserInput.Background = SystemColors.ControlBrush;
                textBlock.UserInput.IsReadOnly = true;
            }
            else
            {
                textBlock.UserInput.Background = SystemColors.WindowBrush;
                textBlock.UserInput.IsReadOnly = false;
            }
        }

        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(ExtendedTextBlock));
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(ExtendedTextBlock));
        public static readonly DependencyProperty HelpTextProperty =
            DependencyProperty.Register("HelpText", typeof(string), typeof(ExtendedTextBlock));
        public static readonly DependencyProperty IsReadOnlyProperty =
            DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(ExtendedTextBlock),
            new FrameworkPropertyMetadata(true, new PropertyChangedCallback(OnIsReadOnlyChanged)));
     }
}